from utils.sqlutils import SqlUtils
from sqlmap.usersqlmap import SqlMap as UserSqlMap


class User:
    def __init__(self):
        self.sqlutils = SqlUtils()

    def user_list(self):
        data = self.sqlutils.execute(UserSqlMap.get("list"))
        user_list = []
        for row in data.get("content"):
            user = {}
            user["id"] = row[0]
            user["username"] = row[1]
            user["image_path"] = row[2]
            user["viewname"] = row[3]
            user_list.append(user)
        return user_list

    def user_info(self):
        return self.sqlutils.execute(UserSqlMap.get("info"))

    def user_create(self, **kwargs):
        return self.sqlutils.execute(UserSqlMap.get("create").format(**kwargs))

    def user_delete(self, **kwargs):
        return self.sqlutils.execute(UserSqlMap.get("delete").format(**kwargs))

    def user_edit(self, **kwargs):
        return self.sqlutils.execute(UserSqlMap.get("edit").format(**kwargs))

    def user_query(self):
        data = self.sqlutils.execute(UserSqlMap.get("query"))
        user_list = []
        for row in data.get("content"):
            user = {}
            user["id"] = row[0]
            user["username"] = row[1]
            user["image_path"] = row[2]
            user["viewname"] = row[3]
            user["sex"] = row[4]
            user["department"] = row[5]
            user["profession"] = row[6]
            user["is_active"] = row[7]
            user["school_id"] = row[8]
            user_list.append(user)
        return user_list
    
    def user_find_id(self,id):
        data = self.sqlutils.execute(UserSqlMap.get("findbyid"))
        for row in data.get("content"):
            user = {}
            user["id"] = row[0]
            user["username"] = row[1]
            user["image_path"] = row[2]
            user["viewname"] = row[3]
            user["sex"] = row[4]
            user["department"] = row[5]
            user["profession"] = row[6]
            user["is_active"] = row[7]
            user["school_id"] = row[8]
            return user
        
    def user_save(self,**kwargs):
        return self.sqlutils.execute(UserSqlMap.get("save").format(**kwargs))

if __name__ == '__main__':
    user = User()
    user.user_create(username="mx",image_path="./storage/mx.jpg",viewname="孟翔")
    # print(user.user_list())
